<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:c="livcos.org/ns/cosmos"
  xmlns:doc="livcos.org/ns/doc"
  xmlns:ddl="livcos.org/ns/db/ddl"
  xmlns:pm="livcos.org/ns/pm"
  xmlns:sd="livcos.org/ns/design/system"
  xmlns:f="livcos.org/ns/xslt/function"
  xmlns="http://www.w3.org/1999/xhtml"
  xmlns:v="livcos.org/ns/view"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  exclude-result-prefixes="c doc ddl pm sd f v xs">
  
	<xsl:import href="../../../../../base.xsl"/>
	
	<xsl:template name="ddl_doc">
		<xsl:variable name="desc" select="* except (ddl:* | sd:*)"/>
		<xsl:if test="$desc">
			<v:chapter>
				<v:title>Beschreibung</v:title>
				<xsl:apply-templates select="$desc"/>
			</v:chapter>
		</xsl:if>
	</xsl:template>
	
	<xsl:template name="ddl_constraints">
		<xsl:if test="ddl:unique | ddl:check">
			<v:chapter>
				<v:title>Constraints</v:title>
				<table cellspacing="0" cellpadding="4" border="1">
					<thead>
						<tr>
							<th>Constraint</th>
							<th>Type</th>
							<th>Felder</th>
							<th>Beschreibung</th>
						</tr>
					</thead>
					<tbody>
						<xsl:apply-templates select="ddl:unique | ddl:check">
							<xsl:sort select="@name"/>
						</xsl:apply-templates>
					</tbody>
				</table>
			</v:chapter>
		</xsl:if>
	</xsl:template>
	
	<xsl:template match="ddl:unique | ddl:check">
		<tr id="{@id}">
			<td>
				<xsl:value-of select="@name" />
			</td>
			<td>
				<xsl:value-of select="local-name(.)" />
			</td>
			<td>
				<xsl:apply-templates select="ddl:field" mode="list">
					<xsl:sort select="@ref-name" />
				</xsl:apply-templates>
			</td>
			<td>
				<xsl:apply-templates select="doc:*"/>
				<xsl:if test="not(doc:*)">&#160;</xsl:if>
			</td>
		</tr>
	</xsl:template>
	
	<xsl:template match="ddl:field" mode="list">
		<xsl:if test="position() > 1"><br/></xsl:if>
		<xsl:variable name="fld" select="../../ddl:field[@name = current()/@ref-name]"/>
		<a href="#{$fld/@id}"><xsl:value-of select="$fld/@name"/></a>
	</xsl:template>
	
	<xsl:template match="ddl:*" mode="ddl_type">
		<span class="text_type">
			<xsl:value-of select="@dbtype"/>
			<xsl:if test="not(@dbtype)">
				<xsl:value-of select="@type"/>
			</xsl:if>
			<xsl:apply-templates select="@size"/>
		</span>
	</xsl:template>
	
	<xsl:template match="ddl:*/@size">
		<xsl:choose>
			<xsl:when test="../@scale">
				<xsl:text>(</xsl:text>
				<xsl:value-of select=". - ../@scale"/>,<xsl:value-of select="../@scale"/>
				<xsl:text>)</xsl:text>
			</xsl:when>
			<xsl:otherwise>(<xsl:value-of select="."/>)</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	
	<xsl:template match="ddl:def">
		<pre class="code block"><xsl:value-of select="."/></pre>
	</xsl:template>
		
	<xsl:template name="ddl-search">
		<xsl:param name="label"/>
		<xsl:param name="query" as="xs:string"/>
		<xsl:variable name="ddl-index" select="resolve-uri('../DefIndex', f:object-id(.))"/>
		<xsl:choose>
			<xsl:when test="f:object-available($ddl-index)">
				<v:choose>
					<v:label><xsl:sequence select="$label"/></v:label>
					<v:link image="/livcos.org/web/img/search.gif"
						url="{f:caller-uri('/livcos.org/web/admin/search.html')}?idx={$ddl-index}&amp;q={$query}"
						description="Search DB object definitions for '{@name}'"/>
				</v:choose>
			</xsl:when>
			<xsl:otherwise>
				<xsl:sequence select="$label"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	
</xsl:stylesheet>
